<?php

class entrant
{
	var $carNo;
	var $driverID;
	var $driverName;
	var $teamID;
	var $teamName;
	var $showCarNo;
	var $carID;
	var $carName;
	var $image;
	var $imageThmb;
	var $teammates;
	
	function entrant($carNo, $driverID, $driverName, $teamID, $teamName, $carID, $carName, $showCarNos, $image = '', $imageThmb = '', $teammates = Array())
	{
		$this->carNo = $carNo;
		$this->showCarNo = $showCarNos;
		$this->driverID = $driverID;
		$this->driverName = $driverName;
		$this->teamID = $teamID;
		$this->teamName = $teamName;
		$this->carID = $carID;
		$this->carName = $carName;
		$this->image = $image;
		$this->imageThmb = $imageThmb;
		$this->teammates = $teammates;
	}
	
	function render()
	{
		global $lang;
		
		$carNo = $this->carNo == 0 || $this->showCarNo == 0 ? '' : $this->carNo . ' ';
		
		if(sizeof($this->teammates) > 0)
		{
			$driver = $carNo . ' <a href="drivers.php?id=' . $this->driverID . '">' . $this->driverName . ' (' . $lang['CaptainInitial'] . ')</a>, ';
			
			foreach($this->teammates as $teammate)
			{
				$driver .= '<a href="drivers.php?id=' . $teammate['user_id'] . '">' . $teammate['user_drivername'] . '</a>, ';
			}
			
			$driver = substr($driver, 0, -2);
		}
		else
		{
			$driver = '<a href="drivers.php?id=' . $this->driverID . '">' . $carNo . $this->driverName . '</a>';
		}
		$team = $this->teamID == '' ? $lang['Independent'] : '<a href="teams.php?id=' . $this->teamID . '">' . stripslashes($this->teamName) . '</a>';
		$car = $this->carID == '' ? $lang['Unassigned'] : '<a href="cars.php?id=' . $this->carID . '">' . $this->carName . '</a>';
		$image = $this->imageThmb == '' ? '' : '<a href="' . $this->image . '" rel="lightbox[champimages]" title="' . 
					$carNo . $this->driverName . " " . $this->carName . '"><img src="' . $this->imageThmb . '"/></a>';
		global $skin_dir;
		include $skin_dir . 'tpl/divisionEntrant.tpl';
	}
}


class empty_entrant
{
	
	function empty_entrant()
	{
	}
	
	function render()
	{	
		$driver = '';
		$team = '';
		$car = '';
		$image = '';
		global $skin_dir;
		include $skin_dir . 'tpl/divisionEntrant.tpl';
	}
}

class division_details
{
	var $mode, $multiDrivers;
	var $div, $champid;
	
	var $items = Array();

	function division_details($div, $mode, $champid = 0, $multiDrivers = 0)
	{
		$this->mode = $mode;
		$this->div = $div;
		$this->champid = $champid;
		$this->multiDrivers = $multiDrivers == 0 ? 0 : 1;
		$this->setup_data();
	}
	
	function render()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
	}
		
	function setup_data()
	{
		global $db, $lang;
		include_once('includes/renderInclude.php');
		
		if($mode = 'entrants')
		{
			$divid = $this->div == 0 ? 0 : $this->div['d_id'];
			$q = 'select cs.cs_no, u.user_id, pf_drivername as user_drivername, c.c_carNumbers, cs_image, 
						 cs_imageThmb, cs_entryOwner, (cs_driver = cs_entryOwner) as entryOwner,' .
					'(SELECT t.t_id FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs.cs_team LIMIT 0, 1) AS t_id,'.
					'(SELECT t.t_name FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs.cs_team LIMIT 0, 1) AS t_name,'.
					'(SELECT ca.c_manufacturer FROM ' . CARS_TABLE . ' ca WHERE ca.c_id = cs.cs_car LIMIT 0, 1) AS c_manufacturer,'.
					'(SELECT ca.c_model FROM ' . CARS_TABLE . ' ca WHERE ca.c_id = cs.cs_car LIMIT 0, 1) AS c_model,'.
					'(SELECT ca.c_id FROM ' . CARS_TABLE . ' ca WHERE ca.c_id = cs.cs_car LIMIT 0, 1) AS car_id '.
					'from ' . USERS_TABLE . ' u, '. DRIVERNAME_TABLE . ' d, ' . CHAMPSIGNUPS_TABLE . ' cs, ' . CHAMPIONSHIPS_TABLE . ' c ' .
					'where cs.cs_driver = u.user_id AND d.user_id = u.user_id AND cs.cs_champ = c.c_id AND cs_div = ' . $divid  . 
					' AND cs_champ = ' . $this->champid . 
					' ORDER BY entryOwner DESC, cs.cs_no ASC, pf_drivername ASC';

			$count = 0;
			$resultsArray = Array();		
			$indexArray = Array();
			$result = $db->sql_query($q);
			while($row = $db->sql_fetchrow($result)) 
			{
				if($this->multiDrivers == 0 || $row['user_id'] == $row['cs_entryOwner'])
				{
					$row['teammates'] = Array();
					$resultsArray[$count] = $row;
					$indexArray[$row['user_id']] = $count;
					$count++;
				}
				else
				{
					$resultsArray[$indexArray[$row['cs_entryOwner']]]['teammates'][] = $row;
				}
			}
			
			
			$firstRow = true;
			$entCount = 0;
			foreach($resultsArray as $row)
			{
				if($firstRow)
				{
					$this->items[] = new addMessage('', ($divid  == 0 ? $lang['Unassigned'] : $this->div['d_title']) . ' (' .$count . 
										($divid == 0 ? '' : '/' . $this->div['d_signuplimit']) . ' ' . $lang["entrants"] . ')', true, true);
					$this->items[] = new renderInclude('tpl/tableStart.tpl');
					$firstRow = false;
				}
				if($entCount == 0)
				{
					$this->items[] = new renderInclude('tpl/rowStart.tpl');
				}
				if($entCount == 2)
				{
					$this->items[] = new renderInclude('tpl/rowEnd.tpl');
					$entCount = 0;
				}
				
				$this->items[] = new entrant($row['cs_no'], $row['user_id'], $row['user_drivername'], $row['t_id'], $row['t_name'], 
											$row['car_id'], $row['c_manufacturer'] . ' ' . $row['c_model'], $row['c_carNumbers'], 
											$row['cs_image'], $row['cs_imageThmb'], $row['teammates']);
											
				$entCount++;
				
			}
			if($entCount == 1)
			{
				$this->items[] = new empty_entrant();
			}
			if(!$firstRow)
			{
				$this->items[] = new renderInclude('tpl/rowEnd.tpl');
				$this->items[] = new renderInclude('tpl/tableEnd.tpl');
			}
		}
	}

}


?>